package com.example.l.zcq1;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import  android.support.v4.app.Fragment;
import android.support.v4.view.LayoutInflaterCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class ceshi1 extends  Fragment{

    //定义的集合对象
    private ListView listView1;
    private ListView listView2;
    //固定的列表内容
    private String[] title=new String[]{"空气温度值","土壤湿度值","CO₂浓度值","光照强度值"};
    private String[] title2=new String[]{"用户密码设置"};
    //数据适配器
    private MyAdapter myAdapter1;
    private MyAdapter2 myAdapter2;
    //创建并且执行视图
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle){
        //设置主界面的对象
        View view=inflater.inflate(R.layout.ceshi_main,viewGroup,false);
        //根据主界面对象获取当中的控件
        listView1=(ListView)view.findViewById(R.id.ceshi_main_listview1);
        listView2=(ListView)view.findViewById(R.id.ceshi_main_listview2);
        //将数据添加到适配器当中
        myAdapter1=new MyAdapter(title);
        myAdapter2=new MyAdapter2(title2);
        //给列表添加适配器
        listView1.setAdapter(myAdapter1);
        listView2.setAdapter(myAdapter2);
        //给列表当中的每一行添加点击事件
        listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                dialog(position);
            }
        });
        listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view2, int position2, long id) {
                dialog2(position2);
            }
        });
        return  view;
    }
    //数据适配器：重点将数据与控件进行绑定
    public class MyAdapter extends BaseAdapter{
        //接受外界传入的数据
        private String[] titles;
        //通过构成函数将外界的值与内部的变量进行交互
        private MyAdapter(String[] title){
            this.titles=title;
        }
        //获取传入参数的长度
        @Override
        public  int getCount() {return this.titles.length;}

        @Override
        public Object getItem(int position)  { return null;}

        @Override
        public long getItemId(int position) { return 0;}

        //获取并返回数据绑定后的视图
        @Override
        public View getView(int position,View convertView,ViewGroup parent){
            //声明视图对象获取列表当中的子项内容
            View view=LayoutInflater.from( getActivity()).inflate(R.layout.ceshi_main_item,null);
            TextView textView=view.findViewById(R.id.ceshi_main_item_title);
            textView.setText(titles[position]);
            return  view;
        }
    }
    //数据适配器2：重点将数据与控件进行绑定
    public class MyAdapter2 extends BaseAdapter{
        //接受外界传入的数据
        private String[] titles;
        //通过构成函数将外界的值与内部的变量进行交互
        private MyAdapter2(String[] title2){
            this.titles=title2;
        }
        //获取传入参数的长度
        @Override
        public  int getCount() {return this.titles.length;}

        @Override
        public Object getItem(int position)  { return null;}

        @Override
        public long getItemId(int position) { return 0;}

        //获取并返回数据绑定后的视图
        @Override
        public View getView(int position,View convertView,ViewGroup parent){
            //声明视图对象获取列表当中的子项内容
            View view2=LayoutInflater.from( getActivity()).inflate(R.layout.ceshi_main_item,null);
            TextView textView=view2.findViewById(R.id.ceshi_main_item_title);
            textView.setText(titles[position]);
            return  view2;
        }
    }

    //通过模板窗体
    public void dialog(int position){
        //声明一个对话框模式窗体
        AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
        //设置弹出窗体的标题
        builder.setTitle("设置报警值："+title[position]);
        //弹出窗体内的信息
        String strl="最小值";
        String str2="最大值";
        int int1=1,int2=2;
        //设置窗体标签的详细说明
        switch (position){
            case 0:
                int1=12;
                int2=24;
                strl+="(℃)";
                str2+="(℃)";
                break;
            case 1:
                int1=122;
                int2=244;
                strl+="(%)";
                str2+="(%)";
                break;
            case 2:
                int1=122;
                int2=244;
                strl+="(ppm)";
                str2+="(ppm)";
                break;
            case 3:
                int1=122;
                int2=244;
                strl+="(Klx)";
                str2+="(Klx)";
                break;
        }
        //获取并设置模板对象
        View view=LayoutInflater.from(getActivity()).inflate(R.layout.dialog,null);
        //根据对象获取模板当中控件
        final EditText editText1=(EditText)view.findViewById(R.id.dialog_edittext1);
        final EditText editText2=(EditText)view.findViewById(R.id.dialog_edittext2);
        TextView textView1=(TextView)view.findViewById(R.id.dialog_textview_title1);
        TextView textView2=(TextView)view.findViewById(R.id.dialog_textview2);
        //给控件赋值
        textView1.setText(strl);
        textView2.setText(str2);
        //由数字转换成字符串可以在变量前加双引号为空的值
        editText1.setText(""+int1);
        editText2.setText(""+int2);
        //将设置好的控件加载到窗体中
        builder.setView(view);
        //当选择弹出窗体的“确认”按钮后所触发的事件
        builder.setPositiveButton("确定",new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog,int which){
                String tempStr=editText1.getText().toString();
            }
        });
        //当选择窗体的“取消”按钮后所触发的事件
        builder.setNegativeButton("取消",null);
        //创建窗体并显示
        builder.create().show();
    }
    //通过模板窗体2
    public void dialog2(int position) {
        //声明一个对话框模式窗体
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        //设置弹出窗体的标题
        builder.setTitle("修改密码：" + title2[position]);
        //弹出窗体内的信息
        String strl = "先前的密码";
        String str2 = "修改后的密码";
        int int1 = 1, int2 = 2;
        //设置窗体标签的详细说明
        switch (position) {
            case 0:
                int1 = 12;
                int2 = 24;
                break;
        }
        //获取并设置模板对象
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog, null);
        //根据对象获取模板当中控件
        final EditText editText1 = (EditText) view.findViewById(R.id.dialog_edittext1);
        final EditText editText2 = (EditText) view.findViewById(R.id.dialog_edittext2);
        TextView textView1 = (TextView) view.findViewById(R.id.dialog_textview_title1);
        TextView textView2 = (TextView) view.findViewById(R.id.dialog_textview2);
        //给控件赋值
        textView1.setText(strl);
        textView2.setText(str2);
        //由数字转换成字符串可以在变量前加双引号为空的值
        editText1.setText("" + int1);
        editText2.setText("" + int2);
        //将设置好的控件加载到窗体中
        builder.setView(view);
        //当选择弹出窗体的“确认”按钮后所触发的事件
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String tempStr = editText1.getText().toString();
            }
        });
        //当选择窗体的“取消”按钮后所触发的事件
        builder.setNegativeButton("取消", null);
        //创建窗体并显示
        builder.create().show();
    }

}
